package com.lagou.module5_5;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
 * @Author wsl
 * @Date 2021/8/10 19:56
 * @Project_Name javase
 */
public class StreamDemo {

    public static void main(String[] args) {
        /**
         * 一、有如下7个元素黄药师，冯蘅，郭靖，黄蓉，郭芙，郭襄，郭破虏，
         * 使用Stream将以郭字开头的元素存入新数组使用Stream将以郭字开头的元素存入新数组
         */

        //1.通过Stream接口的静态方法来获取流
        Stream<String> stream = Stream.of("黄药师", "冯蘅", "郭靖", "黄蓉", "郭芙", "郭襄", "郭破虏");

        //2.方式一：使用Stream将以郭字开头的元素存入新数组
        /*String[] guos = stream.filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("郭");
            }
        }).toArray(String[]::new);
        for (String str: guos){
            System.out.print(str + " ");
        }*/

        System.out.println("-----------------------------");
        //3.方式二：使用Lambda表达式实现
        String[] guos1 = stream.filter(s -> s.startsWith("郭")).toArray(String[]::new);
        for (String str: guos1){
            System.out.print(str + " ");
        }


    }
}
